//
//  QXAppDelegate.m
//  QXBlog
//
//  Created by QinXin on 15/7/4.
//  Copyright © 2015年 labi3285_Lab. All rights reserved.
//



#import "QXAppDelegate.h"
#import "QXAccount.h"
#import "QXTestVC.h"
#import "QXGuideVC.h"
#import "QXOAuthVC.h"
#import "QXMainVC.h"

@interface QXAppDelegate ()

@end

@implementation QXAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    self.window.backgroundColor = [UIColor whiteColor];

    self.window.rootViewController = [self getInitialRootViewConroller];
    
    [self.window makeKeyAndVisible];
    
    [self setupAppearance];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeViewController:) name:@"QXAppDelegateNeedsChangeViewController" object:nil];
    
    return YES;
}

/// 设置全局外观
- (void)setupAppearance {
    [UINavigationBar appearance].tintColor = [UIColor orangeColor];
    [UITabBar appearance].tintColor = [UIColor orangeColor];
}

/// 更新控制器
- (void)changeViewController: (NSNotification *)notice {
    Class cls = notice.userInfo[@"class"];
    self.window.rootViewController = [[cls alloc] init];
}

// 选择初始控制器
- (UIViewController *)getInitialRootViewConroller {
    
    // 检测版本
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
    NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"lastVersion"];

    if (![currentVersion isEqualToString:lastVersion]) {
        [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:@"lastVersion"];
        return [[QXGuideVC alloc] init];
    }
    
    if ([QXAccount sharedAccount] != nil) {
        return [[QXMainVC alloc] init];
    }
    return [[QXOAuthVC alloc] init];
}

@end
